package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.entity.PostsInfo;
import com.wclass.entity.PostsLike;
import com.wclass.mapper.PostsInfoMapper;
import com.wclass.mapper.PostsLikeMapper;
import com.wclass.service.IPostsInfoService;
import com.wclass.service.IPostsLikeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 帖子点赞记录 服务实现类
 * </p>
 *
 * @author SEN
 * @since 2018-12-27
 */
@Service
public class PostsLikeServiceImpl extends ServiceImpl<PostsLikeMapper, PostsLike> implements IPostsLikeService {

    @Autowired
    private IPostsInfoService postsInfoService;

    @Override
    public boolean insert(PostsLike entity) {
        if (entity.getPostsId() != null) {
            //更新点赞数
            PostsInfo postsInfo = new PostsInfo();
            postsInfo.setLikeNum(baseMapper.selectCount(new EntityWrapper<PostsLike>().eq("posts_id",
                    entity.getPostsId())) + 1);
            postsInfo.setId(entity.getPostsId());
            postsInfoService.updateById(postsInfo);
        }
        return super.insert(entity);
    }

    @Override
    public boolean delete(Wrapper<PostsLike> wrapper) {
        if (wrapper.getParamNameValuePairs().get("posts_id") != null) {
            //更新点赞数
            PostsInfo postsInfo = new PostsInfo();
            postsInfo.setLikeNum(baseMapper.selectCount(new EntityWrapper<PostsLike>().eq("posts_id",
                    wrapper.getParamNameValuePairs().get("posts_id"))) - 1);
            postsInfo.setId((long) wrapper.getParamNameValuePairs().get("posts_id"));
            postsInfoService.updateById(postsInfo);
        }
        return super.delete(wrapper);
    }

    @Override
    public Page<PostsLike> selectUnreadPostsLikePage(Page<PostsLike> page, Long userId) {
        return page.setRecords(this.baseMapper.selectUnreadPostsLikePage(page, userId));
    }

}
